# -*-coding:utf-8-*-

from pyrestful import mediatypes
from pyrestful.rest import get
from handlers.base import BaseHandler
from utils.auth import authenticated
import time
from datetime import datetime
from tornado.gen import coroutine


# 1. user is residential admin?
# 2. r_admin support CSV manner for import rooms
# 3. normal user can't import rooms
# 4. devices 表中需要增加的 fields: { building_label, owner(r_admin)[一小区仅支持一登录帐号], residential_id }
# 5. 小区管理员需要做的操作：关联 device_name(label) 与 building_label(需要保证 device_name 唯一)

# GET  /platform/api/rooms?access_token=ACCESS_TOKEN
# PUT  /platform/api/rooms?access_token=ACCESS_TOKEN - change password etc.

class RoomsHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(RoomsHandler, self).__init__(application, request, **kwargs)
        self.areas_dao = self.settings['daos']['areas']

    def check_permissions(self, permissions):
        return True

    @get(_path='/platform/api/rooms', _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    @coroutine
    def get_room_list(self):
        user = yield self.users_dao.get_user_by_id(self.app, self.user_id)
        # result = rooms_dao.get_room_list_by_phone_of_owner(user.get_phone())
        result = self.areas_dao.get_room_list_by_phone_of_owner(user.phone)
        self.respond_success(result)

    @get(_path='/tmp/api/misc/time', _produces=mediatypes.APPLICATION_JSON)
    def get_time(self):
        result = {
            'sec': int(time.time()),
            'usec': datetime.now().microsecond,
        }
        self.finish(result)
